<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @name 		MY_Profiler Class
 * @author 		SimpleCMS Development Team
 * @package 	SimpleCMS
 * @subpackage 	Libraries
 */
class MY_Model extends CI_Model {

    function __construct()
    {    
		parent::__construct();
    }
    

	/*
	 * Generic Model Function
	 */

	function _count_record($params = array('table' => NULL, 'where' => NULL))
	{
		extract($params);
		if($table !== NULL)
		{
			if($where !== NULL)
			{
				$this->db->where($where);
			}
			return $this->db->count_all_results($table);
		}
		return FALSE;
	}

	function _get_record($params = array('table' => NULL, 'condition' => NULL, 'limit' => NULL, 'offset' => NULL, 'fetch' => 'all', 'order_by' => NULL))
	{
		extract($params);
		if($table !== NULL)
		{
			$this->db->from($table);

			if(isset($condition) AND $condition !== NULL)
			{
				$this->db->where($condition);
			}

			if(isset($order_by) AND $order_by !== NULL)
			{
				$this->db->order_by($order_by);
			}

			if(isset($limit) AND $limit)
			{
				$offset = !isset($offset) ? 0:$offset;
				$this->db->limit($limit, $offset);
			}

			$sql = $this->db->get();
			if($sql->num_rows)
			{
				$fetch = !isset($fetch) ? 'all':$fetch;
				if($fetch == 'all')
				{
					return $sql->result();
				}
				else if($fetch == 'row')
				{
					return $sql->row();
				}
			}
		}
		return FALSE;
	}

	function _save_record($params = array('table' => NULL, 'fields' => NULL, 'condition' => NULL, 'return_id' => TRUE))
	{
		extract($params);
		if($table AND $fields)
		{
			if(isset($condition) AND $condition)
			{
				return $this->db->update($table, $fields, $condition);
			}
			else
			{
				if($this->db->insert($table, $fields))
				{
					if(isset($return_id) AND $return_id)
					{
						return $this->db->insert_id();
					}
					else
					{
						return TRUE;
					}
				}
			}
		}
		return FALSE;
	}

	function _delete_record($params = array('table' => NULL, 'condition' => NULL))
	{
		extract($params);
		if($table AND $condition)
		{
			return $this->db->delete($table, $condition);
		}
		return FALSE;
	}
}